package com.shiro.service.impl;

import com.shiro.mapper.RoleMapper;
import com.shiro.mapper.UserRoleMapper;
import com.shiro.pojo.Role;
import com.shiro.pojo.User;
import com.shiro.service.RoleService;
import com.shiro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserService userService;




    @Override
    public Set<String> listRoleNames(String userName) {
        Set<String> result = new HashSet<String>();
        List<Role> roles = listRoles(userName);
        for(Role role : roles){
            result.add(role.getName());
        }
        return result;
    }

    @Override
    public List<Role> listRoles(String userName) {
        List<Role> roles = new ArrayList<>();
        User user = userService.getByName(userName);
        if(null == user)
            return roles;
        roles = roleMapper.listByUid(user.getId());
        return roles;
    }

    @Override
    public List<Role> list() {
        return roleMapper.listOrderById();
    }

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }
}
